package Main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import EventManager.EventManager;
import Gui.GameFrame;

public class JFrameGameWindow extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5637314411743863012L;
	private JMenuBar gameMenuBar;
	private JMenuItem gameNewMenuItem;
	private JMenuItem gameAboutItem;
	private JMenuItem gameInstructionsItem;
	private JMenuItem gameExitMenuItem;
	private JMenuItem gameOptionsItem;
	private void buildMenu()
	{
		gameMenuBar = new JMenuBar();
		setJMenuBar(gameMenuBar);
		JMenu jmenu = new JMenu("Game");
		jmenu.setMnemonic(KeyEvent.VK_G);
		
		gameNewMenuItem = new JMenuItem("New Game", KeyEvent.VK_N);
		
		gameNewMenuItem.addActionListener(this);
		jmenu.add(gameNewMenuItem);
		gameOptionsItem = new JMenuItem("Options", KeyEvent.VK_P);
		
		gameOptionsItem.addActionListener(this);
		jmenu.add(gameOptionsItem);
		gameExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
		gameExitMenuItem.addActionListener(this);
		jmenu.add(gameExitMenuItem);
		gameMenuBar.add(jmenu);
		JMenu jmenu2 = new JMenu("Help");
		jmenu2.setMnemonic(KeyEvent.VK_H);
		gameInstructionsItem = new JMenuItem("Instructions",KeyEvent.VK_I);
		gameInstructionsItem.addActionListener(this);
		jmenu2.add(gameInstructionsItem);
		gameAboutItem = new JMenuItem("About",KeyEvent.VK_A);
		gameAboutItem.addActionListener(this);
		jmenu2.add(gameAboutItem);
		
		gameMenuBar.add(jmenu2);
		add(gameMenuBar);
	}
	public void init()
	{
//		setLayout(new BorderLayout());
////		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
////		int height = screenSize.height;
////		int width = screenSize.width;
////		setSize(width / 2, height / 2);
//		buildMenu();
//		
//		setContentPane(GameFrame.getInstance());
//	//	add(GameFrame.getInstance(), BorderLayout.CENTER);
//		//Thread t = new Thread(new PayDayMain());
//		//t.start();
		
	}
	public JFrameGameWindow(){
		setName("PayDay with Java!");
		setLayout(new BorderLayout());	
		buildMenu();
		add(GameFrame.getInstance());
		pack();
		if(getTitle() != null){
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + Defines.ICON_WIDTH;
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent actionevent)
	{
		/**
		 * Exits the game
		 */
		if (actionevent.getSource() == gameExitMenuItem)
		{
			System.exit(0); // REPLACE WITH m_running = false
		}
		else if (actionevent.getSource() == gameNewMenuItem)
		{
			EventManager.getInstance().queueEvent("EVENT_START_GAME");
		}
		else if (actionevent.getSource() == gameOptionsItem)
		{
			EventManager.getInstance().queueEvent("EVENT_OPTIONS_MENU");
		}
		else if (actionevent.getSource() == gameInstructionsItem)
		{
			EventManager.getInstance().queueEvent("EVENT_INSTRUCTIONS_MENU");
		}
		else if (actionevent.getSource() == gameAboutItem)
		{
			EventManager.getInstance().queueEvent("EVENT_ABOUT_LOAD");
		}
		
	}
}
